package GuessNumber;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class win extends JFrame implements ActionListener {
    JButton randnum;
    JLabel label;
    JTextField field;
    JButton confirm;
    int num;
    public win(String name, int x, int y, int m, int n) {
        setBounds(x,y,m,n);
        setTitle(name);
        setVisible(true);
        setLayout(new FlowLayout());
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });
        init();
    }
    private void init() {
        randnum=new JButton("得到一个随机数");
        label =new JLabel();
        field=new JTextField(10);
        confirm=new JButton("确定");
        add(randnum);
        add(label);
        add(field);
        add(confirm);
        randnum.addActionListener(this);
        confirm.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==randnum){
                num= (int) (Math.random()*(99-10+1)+10);
        }
        if (e.getSource()==confirm){
            try{
                int getNum=Integer.parseInt(field.getText());
                if (getNum<num){
                    label.setText("猜小了");
                    field.setText(null);
                }
                else if (getNum>num){
                    label.setText("猜大了");
                    field.setText(null);
                }
                else{
                    label.setText("猜对了！");
                }
            }catch (Exception exception){
                exception.printStackTrace();
                //JOptionPane.showMessageDialog(this,"你输入了非数字字符！","警告!",JOptionPane.WARNING_MESSAGE);
                int n=JOptionPane.showConfirmDialog(this,"确认输入是否正确","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
                if (n==JOptionPane.YES_OPTION){

                }else if (n==JOptionPane.NO_OPTION){

                }else {
                }
            }
        }
    }
}

public class GuessNumber {
    public static void main(String[] args) {
        win win = new win("猜数字", 200, 200, 400, 300);

    }
}
